package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //1.创建一个对象数组,用于存储数据
    public static Teacher[] arr = new Teacher[5];

    static{
        Teacher tea1 = new Teacher("heima001","张老师","33","1987-11-11");
        Teacher tea2 = new Teacher("heima002","孙老师","34","1988-12-12");
        arr[0]=tea1;
        arr[1]=tea2;
    }

    public boolean addTeacher(Teacher teacher) {

        //2.数据要存储在数据为null的索引,判断数据为null的索引
        //2.1 假设数据都存满了
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]== null){
                index = i;
                break;
            }
        }


        if(index==-1){
            return false;
        }else{
            //3.将数据存储到对应的索引位置
            arr[index] = teacher;
            return true;
        }
    }


    public Teacher[] isExit() {
       return arr;
    }



    public void deleteTeacherByid(String delId) {
        //1.查询id在数组中所对应的索引位置
        int index = getIndex(delId);

        //2.将索引位置,使用null进行替换
        arr[index]=null;

    }



    //因为修改的时候也要使用
    public int getIndex(String id){
        int a = -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null&&arr[i].getId().equals(id)){
                a = i;
                break;
            }
        }
        return a;
    }

    public void updateTeacher(String updateId, Teacher newTea) {
        //1.查询id在数组中的索引位置
        int index = getIndex(updateId);

        //2.将索引位置,使用新的老师对象进行替换
        arr[index]=newTea;
    }
}
